/*
 * Created on 2005-6-25
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.linkage.lcims.base.dao.system.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.linkage.lcims.base.dao.HibernateBaseDAOImpl;
import com.linkage.lcims.base.dao.system.interfaces.AdminLogDAO;
import com.linkage.lcims.base.po.system.MAdminLog;
import com.linkage.util.SystemUtil;
import com.linkage.util.exception.DAOException;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
@SuppressWarnings("unchecked")
public class AdminLogDAOImpl extends HibernateBaseDAOImpl implements
		AdminLogDAO {
	public AdminLogDAOImpl() {
		super();
		// TODO Auto-generated constructor stub
	}
	/* (non-Javadoc)
	 * @see com.linkage.lcims.base.dao.system.interfaces.AdminLogDAO#saveAdminLog(com.linkage.lcims.base.po.system.MAdminLog)
	 */
	public void saveAdminLog(MAdminLog adminLog) throws DAOException {
		this.save(adminLog);
	}

	/* (non-Javadoc)
	 * @see com.linkage.lcims.base.dao.system.interfaces.AdminLogDAO#getAdminLog(java.lang.String)
	 */
	public List getAdminLog(String admin) throws DAOException {
		String hql = "from MAdminLog as log where log.admin=:admin";
		return this.query(hql,"admin",admin);
	}

	/* (non-Javadoc)
	 * @see com.linkage.lcims.base.dao.system.interfaces.AdminLogDAO#deleteAdminLogBySql(java.util.Map)
	 */
	public int deleteAdminLog(Map params) throws DAOException {
		try{
			StringBuffer hql = new StringBuffer();
			hql.append("delete MAdminLog");
			if(!params.isEmpty()){
				Set set = params.entrySet();
				int size = set.size();
				String[] paramNames = new String[size];
				Object[] values = new Object[size];
				Iterator iter = set.iterator();
				int i = 0;
				while(iter.hasNext()){
					Map.Entry me = (Map.Entry)iter.next();
					paramNames[i] = (String)me.getKey();
					values[i] = me.getValue();
					i++;
				}
				hql.append(" where ");
				for(i = 0; i < paramNames.length; i++){
					if(paramNames[i].equals("clienttype") 
							|| paramNames[i].equals("operatetype")){
						hql.append(paramNames[i]).append("=:")
							.append(paramNames[i]).append(" and ");
						values[i] = new Integer((String)values[i]);
					}else if(paramNames[i].equals("boperatetime")){
						hql.append("operatetime>=:")
							.append(paramNames[i]).append(" and ");
						String boperatetime = (String)values[i];
						boperatetime = SystemUtil.formateDateString(boperatetime);
						boperatetime = boperatetime + "-00-00-00";
						values[i] = SystemUtil.getDateFromString(boperatetime);
					}else if(paramNames[i].equals("eoperatetime")){
						hql.append("operatetime<=:")
							.append(paramNames[i]).append(" and ");
						String eboperatetime = (String)values[i];
						eboperatetime = SystemUtil.formateDateString(eboperatetime);
						eboperatetime = eboperatetime + "-23-59-59";
						values[i] = SystemUtil.getDateFromString(eboperatetime);
					}else{
						hql.append(paramNames[i]).append("=:")
							.append(paramNames[i]).append(" and ");
					}
				}
				return this.delete(hql.substring(0, hql.length()-5),paramNames,values);
			}else{
				return this.delete(hql.toString());
			}
		} catch(Exception e){
			throw new DAOException(e, this.getClass());
		}
	}

}
